<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>phs</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="phasorbnk.html" title="phasorbnk" />
    <link rel="next" href="pindex.html" title="pindex" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">phs</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="phasorbnk.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="pindex.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="phs"></a>
      <div class="titlepage"></div>
      <a id="IndexPhs" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">phs</span>
        </h2>
        <p>phs — 
    Obtains the phases of a complex-number array
    </p>
      </div>
      <div class="refsect1">
        <a id="idp134121312"></a>
        <h2>Description</h2>
        <p>
      This opcode returns the phases of a complex-number array, as a real-valued array with half the size of its input plus one. The reason for the extra point is to keep the array size equivalent to the output of the mags opcode (which is often used alongside this one).
    </p>
      </div>
      <div class="refsect1">
        <a id="idp134125408"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">kout[] <span class="command"><strong>phs</strong></span> kin[]</pre>
      </div>
      <div class="refsect1">
        <a id="idp134127584"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>kout[]</em></span> -- output array containing the phases. It will be created if it does not exist.
         </p>
        <p>
      <span class="emphasis"><em>kin[]</em></span> -- input array containing the complex-valued real-imaginary input.
    </p>
      </div>
      <div class="refsect1">
        <a id="idp134189776"></a>
        <h2>Examples</h2>
        <p>
      Here is an example of the phs opcode. It uses the file <a class="ulink" href="examples/phs.csd" target="_top"><em class="citetitle">phs.csd</em></a>.</p>
        <div class="example">
          <a id="idp134191552"></a>
          <p class="title">
            <strong>Example 624. Example of the phs opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
-d -o dac
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>
<span class="op">/</span><span class="op">*</span> <span class="ohdr">ksmps</span> needs to be an integer div of
   hopsize <span class="op">*</span><span class="op">/</span>
<span class="ohdr">ksmps</span> <span class="op">=</span> 64

<span class="oblock">instr</span> 1

 ihopsize <span class="op">=</span> 256  <span class="comment">; hopsize</span>
 ifftsize <span class="op">=</span> 2048 <span class="comment">; FFT size </span>
 iolaps <span class="op">=</span> ifftsize<span class="op">/</span>ihopsize <span class="comment">; overlaps</span>
 ibw <span class="op">=</span> <span class="ohdr">sr</span><span class="op">/</span>ifftsize <span class="comment">; bin bandwidth</span>
 kcnt <span class="opc">init</span> 0    <span class="comment">; counting vars</span>
 krow <span class="opc">init</span> 0

 kOla[] <span class="opc">init</span> ifftsize <span class="comment">; overlap-add buffer</span>
 kIn[] <span class="opc">init</span> ifftsize  <span class="comment">; input buffer</span>
 kOlph[] <span class="opc">init</span> ifftsize<span class="op">/</span>2<span class="op">+</span>1 <span class="comment">; old phases</span>
 kPhs[] <span class="opc">init</span> ifftsize<span class="op">/</span>2<span class="op">+</span>1 <span class="comment">; synthesis phases</span>
 kDeltaOut[] <span class="opc">init</span> ifftsize<span class="op">/</span>2<span class="op">+</span>1 <span class="comment">; synthesis freqs</span>
 kMagsOut[] <span class="opc">init</span> ifftsize<span class="op">/</span>2<span class="op">+</span>1 <span class="comment">; synthesis mags</span>
 kOut[][] <span class="opc">init</span> iolaps, ifftsize <span class="comment">; output buffers</span>

 a1 <span class="opc">diskin2</span> "fox.wav",1,0,1 <span class="comment">; audio input</span>

 <span class="op">/</span><span class="op">*</span> every hopsize samples <span class="op">*</span><span class="op">/</span>
 <span class="octrl">if</span> kcnt <span class="op">==</span> ihopsize then  
   <span class="op">/</span><span class="op">*</span> <span class="opc">window</span> and take FFT <span class="op">*</span><span class="op">/</span>
   kWin[] <span class="opc">window</span> kIn,krow<span class="op">*</span>ihopsize
   kSpec[] <span class="opc">rfft</span> kWin
   
  <span class="op">/</span><span class="op">*</span> <span class="opc">mags</span> <span class="op">&amp;</span> phases <span class="op">*</span><span class="op">/</span>
  kMags[] <span class="opc">mags</span> kSpec 
  kPha[] <span class="opc">phs</span> kSpec
  
  <span class="op">/</span><span class="op">*</span> phase diffs <span class="op">*</span><span class="op">/</span>
  kDelta[] <span class="op">=</span> kPha <span class="op">-</span> kOlph 
  kOlph <span class="op">=</span> kPha
  
  <span class="op">/</span><span class="op">*</span> <span class="opc">pitch</span> shift <span class="op">*</span><span class="op">/</span>
  ki <span class="op">=</span> 0
  iscal <span class="op">=</span> 1.25 <span class="comment">; maj 3rd up</span>
  until ki <span class="op">==</span> ifftsize<span class="op">/</span>2 do
   <span class="octrl">if</span> ki<span class="op">*</span>iscal <span class="op">&lt;</span> ifftsize<span class="op">/</span>2 then
     kDeltaOut[ki<span class="op">*</span>iscal] <span class="op">=</span> kDelta[ki]<span class="op">*</span>iscal
     kMagsOut[ki<span class="op">*</span>iscal] <span class="op">=</span> kMags[ki]
   <span class="octrl">endif</span>
    ki <span class="op">+</span><span class="op">=</span> 1
  od
  
  <span class="op">/</span><span class="op">*</span> accum phases <span class="op">*</span><span class="op">/</span>
   kPhs <span class="op">=</span> kDeltaOut <span class="op">+</span> kPhs 
   kSpec <span class="opc">pol2rect</span> kMagsOut,kPhs
   
   <span class="op">/</span><span class="op">*</span> IFFT <span class="op">+</span> <span class="opc">window</span> <span class="op">*</span><span class="op">/</span>
   kRow[] <span class="opc">irfft</span> kSpec
   kWin <span class="opc">window</span> kRow, krow<span class="op">*</span>ihopsize
   <span class="op">/</span><span class="op">*</span> place it on <span class="opc">out</span> buffer <span class="op">*</span><span class="op">/</span>
   kOut <span class="opc">setrow</span> kWin, krow

   <span class="op">/</span><span class="op">*</span> zero the ola buffer <span class="op">*</span><span class="op">/</span>
   kOla <span class="op">=</span> 0
   <span class="op">/</span><span class="op">*</span> overlap<span class="op">-</span>add <span class="op">*</span><span class="op">/</span>
   ki <span class="op">=</span> 0
   until ki <span class="op">==</span> iolaps do
     kRow <span class="opc">getrow</span> kOut, ki
     kOla <span class="op">=</span> kOla <span class="op">+</span> kRow
     ki <span class="op">+</span><span class="op">=</span> 1
   od
  
  <span class="op">/</span><span class="op">*</span> update counters <span class="op">*</span><span class="op">/</span> 
  krow <span class="op">=</span> (krow<span class="op">+</span>1)<span class="op">%</span>iolaps
  kcnt <span class="op">=</span> 0
 <span class="octrl">endif</span>

 <span class="op">/</span><span class="op">*</span> shift audio <span class="opc">in</span><span class="op">/</span><span class="opc">out</span> of buffers <span class="op">*</span><span class="op">/</span>
 kIn <span class="opc">shiftin</span> a1
 a2 <span class="opc">shiftout</span> kOla
 a1 <span class="opc">delay</span> a1, (ifftsize<span class="op">+</span>ihopsize)<span class="op">/</span><span class="ohdr">sr</span>
    <span class="opc">out</span> a1<span class="op">*</span>0.3<span class="op">+</span>a2<span class="op">/</span>iolaps

 <span class="op">/</span><span class="op">*</span> increment counter <span class="op">*</span><span class="op">/</span>
 kcnt <span class="op">+</span><span class="op">=</span> <span class="ohdr">ksmps</span>

<span class="oblock">endin</span>

<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>
<span class="stamnt">i</span>1 0 10
<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <br class="example-break" />
      </div>
      <div class="refsect1">
        <a id="idp134195712"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="vectorial.html" title="Vectorial Opcodes"><em class="citetitle"></em>Vectorial opcodes</a>,
      <a class="link" href="array.html" title="array"><em class="citetitle"></em>array</a>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp134198720"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Victor Lazzarini</td>
          </tr>
          <tr>
            <td>NUI Maynooth</td>
          </tr>
          <tr>
            <td>2014</td>
          </tr>
        </table>
        <p>
    </p>
        <p>New in version 6.04</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="phasorbnk.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="pindex.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">phasorbnk </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> pindex</td>
        </tr>
      </table>
    </div>
  </body>
</html>
